//  对DAC70508芯片进行配置，从配置文件中读取参数之后进行配置: 
//  dac70508_cfg mode dac70508_cfg.ini state

#include "SchmidtCAM.h"

int main( int argc, char *argv[] ){

	if( argc != 3 ) {
	    printf("usage:\n");
	    printf("\t%s daisy.ini state\n", argv[0]);
		printf("\t state = 0 (off) or state = 1 (on)\n");
		// printf("\t an extra opt \'-a\' can also be appended for quick adjustment of Vout(s) for current value(s).\n");
	    return 0;
	}
	
	char inifile[1024];
    sprintf(inifile, "%s", argv[1]);
	
	int state = atoi(argv[2]);
	
	if( (state !=0) && (state != 1) ){
		char errmsg[1024];
	    sprintf(errmsg," state MUST be 0 or 1 !");
	    printDebugInfo(errmsg);
	    exit(0);
	}

	int fd = open("/dev/mem", O_RDWR | O_SYNC);
	uint *ptr1 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_OUT);
	uint *gpio_reg 		= ptr1;		// 0x41200000
	uint *gpio2_reg		= ptr1+2;	// 0x41200008
	
	uint *ptr2 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_IN);
	uint *gpio_in_reg	= ptr2;		// 0x41210000

	// SPI config
    SPI_CONFIG *spi_config = (SPI_CONFIG*)malloc(sizeof(SPI_CONFIG));
    
    setup_SPI_by_device_name( spi_config, "daisy" );
    
	DAISY_Config *daisy_cfg = (DAISY_Config*)malloc(sizeof(DAISY_Config));
	init_daisy_config( daisy_cfg );

    read_daisy_config( daisy_cfg, inifile );

	// print_daisy_config( daisy_cfg );
	// exit(0);

	// 告诉PL端，daisy Chain中的哪些寄存器的值需要更新
	set_reg_update_option( daisy_cfg, gpio_reg );

	config_daisy_via_SPI(   gpio_reg, 
					        gpio2_reg, 
					        gpio_in_reg,
					        spi_config,
					        daisy_cfg,
							state,
							1 );
    
    free_daisy_config( daisy_cfg );
    free(spi_config);
    
    munmap(ptr1,10);
    munmap(ptr2,10);
    close(fd);
}
